package org.jsola.hr.common.secret.example.RSA;

import org.jsola.hr.common.secret.util.CoderUtil;
import org.jsola.hr.common.secret.util.SecureUtils;

import javax.crypto.Cipher;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

/**
 * @author
 * @create 2018-08-04 13:05
 **/
public class RSADemo1 {
    public static String data="hello world";

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub

//        KeyPair keyPair=genKeyPair(1024);
//
//        //获取公钥，并以base64格式打印出来
//        PublicKey publicKey=keyPair.getPublic();
//        System.out.println("公钥："+new String(CoderUtil.encodeBase64(publicKey.getEncoded())));
//
//        //获取私钥，并以base64格式打印出来
//        PrivateKey privateKey=keyPair.getPrivate();
//        System.out.println("私钥："+new String(CoderUtil.encodeBase64(privateKey.getEncoded())));
//
//        //公钥加密
//        byte[] encryptedBytes=encrypt(data.getBytes(), publicKey);
//        System.out.println("加密后："+new String(encryptedBytes));
//
//        //私钥解密
//        byte[] decryptedBytes=decrypt(encryptedBytes, privateKey);
//        System.out.println("解密后："+new String(decryptedBytes));

        //生成秘钥对
        KeyPair keyPair = SecureUtils.genKeyPair(1024);
        //获取公钥，并以base64格式打印出来
        PublicKey publicKey = keyPair.getPublic();
        String pubKey = new String(CoderUtil.encodeBase64(publicKey.getEncoded()));
        //获取私钥，并以base64格式打印出来
        PrivateKey privateKey = keyPair.getPrivate();
        String priKey = new String(CoderUtil.encodeBase64(privateKey.getEncoded()));

        String dataEnc = SecureUtils.encryptRSAPublic("999.99", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCRxPzVtmOX2bOtUfWZsZGd1fy2Fperx5sPK9YG\n" +
                "Yboh8pmj1okbp2GeWpkb/OYFpJrbP7o6kY7wI3y3eN7hR9FsyE6ShvJM9AP1ApaaNfiuqtic9TX1\n" +
                "Uswsbq2CVPfW7S7fnJK5c2BBU+xwMQEhjZdoQ/1zR1LAWDz9ivwc9UnfbQIDAQAB\n");
        System.out.println("公钥加密数据：" + dataEnc);
        String dataDec = SecureUtils.decryptRSAPrivate(dataEnc, "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJHE/NW2Y5fZs61R9ZmxkZ3V/LYW\n" +
                "l6vHmw8r1gZhuiHymaPWiRunYZ5amRv85gWkmts/ujqRjvAjfLd43uFH0WzITpKG8kz0A/UClpo1\n" +
                "+K6q2Jz1NfVSzCxurYJU99btLt+ckrlzYEFT7HAxASGNl2hD/XNHUsBYPP2K/Bz1Sd9tAgMBAAEC\n" +
                "gYBvR9G4mN9Y+lJHm00CQuOmecjNfVvWQLBoGY/vGUYOSnTt9YZ//Pzx9bny+tLjNfEeNX1G3v4e\n" +
                "5wh7zCNiGgCOgXBURe3cC5QxlqbD63LkWURFRcqVt/jcO184TTLWwZkVaAuuubb21BVTFQM8ggAS\n" +
                "bP8lXIKPPVUydVb1JI2OgQJBANfAllQ5Rb3oYG0q+gTwQFqWpzqvlbemychuNMvi41dMx6IpO/pQ\n" +
                "/Uo4caOsWnuX1ZwzlID7MrykSPbzrmkuSCECQQCs9lGYvxtqXAvkvqnwIH8TTtYPbI0kmK6JL76I\n" +
                "ierUDv5p8W9nmkRIhDkJIuQYy65SXtRX4/xbYhmD4dFZwn3NAkBxRgOnJIqhMieuKSoq/ZhL91zF\n" +
                "Svpn+YFnYbUGdpC1fPIzCUMcIPlaxlejfxHiYc0cvVfyf/AbaoWEYlvnjQ0BAkBCOWnSp1GS7r1r\n" +
                "Dwm4NGkTdnsFmtWL7p3dTxeP5eaw3TDSVawHKgz9CwtfkSluyQXs6fw5zsRAJRaJ5Xun94z9AkAb\n" +
                "Jp47M5IloL6E3hsybS/j/A1u6MPkuueuBzKBzncV2RBLkyxL32Ny8VCG8jxUVBjJ5qFAl/4VH7mw\n" +
                "xnRlt64K\n");
        System.out.println("私钥解密数据：" + dataDec);
    }

    //生成密钥对
    public static KeyPair genKeyPair(int keyLength) throws Exception {
        KeyPairGenerator keyPairGenerator= KeyPairGenerator.getInstance("RSA");
        keyPairGenerator.initialize(1024);
        return keyPairGenerator.generateKeyPair();
    }

    //公钥加密
    public static byte[] encrypt(byte[] content, PublicKey publicKey) throws Exception {
        Cipher cipher= Cipher.getInstance("RSA");//java默认"RSA"="RSA/ECB/PKCS1Padding"
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(content);
    }

    //私钥解密
    public static byte[] decrypt(byte[] content, PrivateKey privateKey) throws Exception {
        Cipher cipher= Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(content);
    }
}